// Created by 11372 on 2021/12/13 11:13
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：4 ms, 在所有 C++ 提交中击败了97.95%的用户
 * 内存消耗：8.4 MB, 在所有 C++ 提交中击败了25.34%的用户
 */
class Solution {
public:
    int minScoreTriangulation(vector<int>& w) {
        int n = w.size();
        vector<vector<int>> f(n, vector<int>(n));
        for (int len = 3; len <= n; len++)
            for (int i = 0; i + len - 1 < n; i++) {
                int j = i + len - 1;
                f[i][j] = 1e9;
                for (int k = i + 1; k < j; k++)
                    f[i][j] = min(f[i][j], f[i][k] + f[k][j] + w[i] * w[k] * w[j]);
            }
        return f[0][n - 1];
    }
};

int main() {

    vector<int> w = {3, 7, 4, 5};
    cout << Solution().minScoreTriangulation(w) << endl;  // 144

    return 0;
}
